package com.liusu.spider.encrypt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liusu.spider.bean.Person;
import com.liusu.spider.bean.Result;
import com.liusu.spider.bean.ResultVO;
import com.liusu.spider.bean.SignToken;
import com.liusu.spider.entity.VideoEntity;
import com.liusu.spider.js.File_;
import com.liusu.spider.js.String_;
import com.liusu.spider.request.Request;
import com.liusu.spider.util.Learn;
import com.liusu.spider.wuzhou.Print;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.util.*;

public class Railgun extends Print {

    public Request request = new Request();
    public Learn learn = new Learn();

    @Test
    public void context(){
        String url = "https://ke.qq.com/webcourse/464930/103765476#taid=12373276794230818&vid=3701925922065222593";
        String[] split = url.split("/|#|=|&");
        /**
         * 课程ID
         */
        String cid = split[4];
        /**
         * 学期ID
         */
        String termId = split[5];
        String taid = split[7];
        /**
         * 视频ID
         */
        String vid = split[9];

        List<String> list = File_.readLines("temporary/cookie.html");
        Map<String,String> cookies = String_.string_to_map(list.get(0));
        String p_lskey = cookies.get("p_lskey");

        String bkn = learn.getBkn(p_lskey)+"";
        /**
         *
         */
        String token_url = f("{0}/cgi-bin/qcloud/get_token?term_id={1}&fileId={2}&bkn={3}&t=0.8631",String_.rsplit(url,"/",3,0),termId,vid,bkn);
        HashMap<String,String> headers = new HashMap<>();
        headers.put("referer",url.split("#")[0]);
        headers.put("cookie",list.get(0));
        /**
         * 登录的token
         */
        String token = request.get(token_url, headers);
        ObjectMapper mapper = new ObjectMapper();
        SignToken signToken = null;
        try {
            signToken = mapper.readValue(token, SignToken.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        print(token);
        Result result = signToken.result;
        Integer exper = result.exper;
        String sign = result.sign;
        String t = result.t;
        String us = result.us;
        Map<String,String> login_map = new HashMap<>();
        login_map.put("exper",exper+"");
        login_map.put("sign",sign);
        login_map.put("t",t);
        login_map.put("us",us);

        String playinfo_url = learn.assemblyPath("1258712167",vid,login_map);
        String playinfo = request.get(playinfo_url);

        print(playinfo);
        VideoEntity videoEntity = null;
        try {
            videoEntity = mapper.readValue(playinfo, VideoEntity.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        String base_first_m3u8_url = videoEntity.videoInfo.masterPlayList.url;


        Map<String,String> course_info_map = new HashMap<>();
        course_info_map.put("termId",termId);
        course_info_map.put("cid",cid);
        course_info_map.put("vod_type","0");

        String hlsToken = learn.getDrmToken(course_info_map, cookies);
        String first_m3u8_url = learn.mergeUrlWithToken(base_first_m3u8_url, hlsToken);
        print(first_m3u8_url);
        print("{0} {1} {2} {3}",cid,termId,taid,vid);
    }


}